#include "includes.h"
#include "spi.h"
#include "w25qxx.h"
#include "ICcard.h"	

#include "usart1.h"
#include "usart2.h"
#include "usart3.h"
#include "usart5.h"
#include <IOdevice.h>
#include "xrj_protocol.h"
#if 0
/*
20 00 20 06 78 6D 7A 6B 68 62 D7 03										模块装载读卡密钥
20 00 23 11 03 78 6D 7A 6B 68 62 FF 07 80 69 00 00 00 00 00 00 D1 03	卡片设置密钥
20 00 26 04 00 00 01 03 DF 03											自动读取1块到3块的数据
*/
static uint8_t CRC_CHECK(uint8_t * from,uint16_t size)
{
uint8_t  ret = 0x00 ;
uint16_t  i;	
		for(i = 0; i < size; i++)
		{
				ret ^= from[i];
		}	
		return ret;
}




INT32 Read_Card(UINT8 *buf, UINT32 len, UINT32 timeout)
{
	return 	USART1_Read(buf,len,timeout);;
}

	UINT8 uart5_tab[100] = {0x00, 0x05, 0x02, 0x05 , 0x0C};
	UINT8 uart5_tab_rx[100] = {0x00,0x00};
	
void UART5_TEST(void)
{
INT32 len;	
INT32 i;
INT32 weight;
UINT32  TMP;
	USART5_Write(uart5_tab,5);
	len = USART5_Read(uart5_tab_rx, sizeof(uart5_tab_rx), 1000);
	if (len > 0)
	{
		printf("USART5 : ");
		for (i = 0; i < len; i++)
		{
			printf(" %02x",uart5_tab_rx[i]);
		}	

		printf("\n");
		TMP = uart5_tab_rx[5];
		weight = TMP<<16;
		TMP = uart5_tab_rx[6];
		weight += TMP<<8;
		weight +=uart5_tab_rx[7];
		
		if (uart5_tab_rx[4] == 0x84)
		{
			weight *= (-2); 
		}
		if (uart5_tab_rx[4] == 0x04)
		{
			weight *= (2);
		}	 
		
		printf("weight %d\n", weight);
		
	}	
}	
	UINT8 uart3_tab[100] = {0x31,0x32,0x33,0x34,0x35,0x00};
	UINT8 uart3_tab_rx[100] = {0x00,0x00};
	
void UART3_TEST(void)
{
//	USART3_DMA_WRITE(uart3_tab,5);
	USART3_Read(uart3_tab_rx, sizeof(uart3_tab_rx), 1000);
	printf("USART3 %s\n",uart3_tab_rx);
}	

#define IC_STX			0
#define IC_SEQNR		1
#define IC_STATUS		2
#define IC_LEN			3

UINT8 IC_RX_Tab[64];
void ICCARD_Task(void *pdata)
{
	
	INT32 len;
	INT32 i;
	UINT32 IC_card;
	UINT32 tmp;
	memset(IC_RX_Tab,0,20);
	while(1)
	{
		len = Read_Card(IC_RX_Tab, sizeof(IC_RX_Tab), 0);
		if (len > 0)
		{
			printf("ICARD[%d]:",len);
			for (i=0; i < len; i++)
				printf(" %02x",IC_RX_Tab[i]);
			printf("\n");
	
#if 1
			if (len == 59)
			{
				if ((IC_RX_Tab[IC_STX] == 0x20)
					&&(IC_RX_Tab[IC_SEQNR] == 0x00)
					&&(IC_RX_Tab[IC_STATUS] == 0x00)
					&&(IC_RX_Tab[IC_LEN] == 0x35)
					&&(IC_RX_Tab[58] == 0x03)
					)
				{
					if ((CRC_CHECK(&IC_RX_Tab[1],56)+ IC_RX_Tab[57]) == 0xff)
					{
						IC_card = 0;
						
						tmp = IC_RX_Tab[8]; 
						IC_card |= (tmp<<24);
						
						tmp = IC_RX_Tab[7];
						IC_card |= (tmp<<16);	
						
						tmp = IC_RX_Tab[6];
						IC_card |= (tmp<<8);	
						
						tmp = IC_RX_Tab[5];	
						IC_card |= (tmp<<0);
						XRJ_mutex_lock(&Device_Value.Lock);
						Device_Value.IC_CARD = IC_card;
						memcpy(Device_Value.IC_BLOCK_2, &IC_RX_Tab[16+9],16);
						XRJ_mutex_unlock(&Device_Value.Lock);
						BEEP_Mode_Set(D_BEEP_ICMODE);
						Send_Report_Message(IOT__UNIT_INDEX__IC_BLOCK_2,NULL);
					}	
				}
			}	
#else			
			if (len == 9)
			{

				if (CRC_CHECK(&tab[1],6) ==  tab[7])
				{
					IC_card = 0;
					
					tmp = tab[3]; 
					IC_card |= (tmp<<24);
					
					tmp = tab[4];
					IC_card |= (tmp<<16);	
					
					tmp = tab[5];
					IC_card |= (tmp<<8);	
					
					tmp = tab[6];	
					IC_card |= (tmp<<0);	
					
					XRJ_mutex_lock(&Device_Value.Lock);
					Device_Value.IC_CARD = IC_card;
					XRJ_mutex_unlock(&Device_Value.Lock);
					BEEP_Mode_Set(D_BEEP_ICMODE);
					Send_Report_Message(IOT__UNIT_INDEX__IC_CARD,NULL);
				}					
			}	
#endif						
		/*	USART3_DMA_WRITE(tab,len);
			USART5_Write(tab,len);*/
			memset(IC_RX_Tab,0,20);
		}	
	}	
}




#endif

